#include "SEL.h"
#include "SEL_resource.h"

using namespace SEL;

int main ()
{
	debug::init (true);

	// test the tokenizer	

	FileRWops *ops = new FileRWops ("testtokens", "r");
	Tokenizer *tokenizer = new Tokenizer (ops, "testtokens");
	while (tokenizer->ok () && !tokenizer->eof ())
	{
		int type;
		std::string value;
		if (tokenizer->nextToken ())
		{
			type = tokenizer->tokenType ();
			value = tokenizer->tokenValue ();
			std::cout << tokenizer->filename () << ":"
			     << tokenizer->lastLine () + 1 << ": `"
			     << value << "' ";
			if (type == Tokenizer::TOKEN_STRING)
				std::cout << "(string)";
			else if (type == Tokenizer::TOKEN_OPERATOR)
				std::cout << "(operator)";
			else if (type == Tokenizer::TOKEN_INVALID)
				std::cout << "(invalid)";
			else 
				std::cout << "(unknown)" ;
			if (value == "include")
			{
				if (!tokenizer->nextToken ())
					continue;
				type = tokenizer->tokenType ();
				value = tokenizer->tokenValue ();
				std::cout << "****** including file " << value << std::endl;
				FileRWops *_ops = new FileRWops (value, "r");
				if (!tokenizer->pushFile (_ops, value))
				{
					std::cout << "! LOOP DETECTED" << std::endl;
				}
				continue;
			}

			if (tokenizer->peekNextToken ())
			{
				std::cout << "  : next - `" << tokenizer->nextTokenValue () << "'";
			}
			std::cout << std::endl;
		}
		else if (!tokenizer->ok ())
		{
			std::cout << "An error occured: ";
			if (tokenizer->error () == "")
				std::cout << "(unknown)";
			else
				std::cout << tokenizer->error ();
			std::cout << std::endl;
		}
		else if (tokenizer->eof ())
		{
			std::cout << "End of file." << std::endl;
		}
	}
	delete tokenizer;
	return 0;
}
